<?php

namespace api\modules\v1\controllers\clinic;

use Yii;
use api\controllers\OnAuthController;
use common\models\clinic\Clinic;
use common\helpers\CommonHelper;
use api\modules\v1\forms\clinic\ClinicForm;

/**
 * Class IndexController
 * @package api\modules\v1\controllers\clinic
 */
class IndexController extends OnAuthController
{
    /**
     * @var Clinic
     */
    public $modelClass = Clinic::class;


    /**
     * @api {get} /v1/clinic/index/list 诊室列表
     * @apiName list
     * @apiGroup Clinic
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.page                   当前页码
     * @apiSuccess {Number}  data.pageSize               每页显示数
     * @apiSuccess {Number}  data.total                  总数
     * @apiSuccess {Array}   data.list                   诊室列表
     * @apiSuccess {Number}  data.list.id                诊室ID
     * @apiSuccess {Number}  data.list.project_id        项目ID
     * @apiSuccess {String}  data.list.name              诊室名称
     * @apiSuccess {String}  data.list.clinic_user       诊室人员
     * @apiSuccess {Number}  data.list.status            状态
     * @apiSuccess {String}  data.list.status_str        状态值
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;
        $page = $params['page'] ?? 0;
        $pageSize = $params['pageSize'] ?? 0;

        // 查询结果
        $data = Yii::$app->services->clinic->getListPage($params, (int)$page, (int)$pageSize);

        // 格式化列表数据
        $data['list'] = Yii::$app->services->clinic->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/clinic/index/info 诊室详情
     * @apiName info
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                                            诊室ID
     *
     * @apiSuccess {Object}     data                                    返回数据
     * @apiSuccess {Number}     data.id                                 诊室ID
     * @apiSuccess {String}     data.name                               诊室名称
     * @apiSuccess {Number}     data.status                             状态
     */
    public function actionInfo()
    {
        $id = (int)Yii::$app->request->get('id', 0);

        // 诊室信息
        $clinic = Yii::$app->services->clinic->findById($id);

        return [
            'id' => $clinic->id ?? 0,
            'name' => $clinic->name ?? '',
            'status' => $clinic->status ?? Clinic::STATUS_ENABLE,
        ];
    }

    /**
     * @api {post} /v1/clinic/index/create 新增诊室
     * @apiName create
     * @apiGroup Clinic
     *
     * @apiParam {String} name                           诊室名称
     * @apiParam {Number} status                         状态【1启用、0停用】
     * @apiParam {Number} sort                           排序
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     诊室ID
     */
    public function actionCreateClinic()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new ClinicForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinic->create($model);
    }

    /**
     * @api {post} /v1/clinic/index/update 编辑诊室
     * @apiName update
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             诊室ID
     * @apiParam {String} name                           诊室名称
     * @apiParam {Number} status                         状态【1启用、0停用】
     * @apiParam {Number} sort                           排序
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     诊室ID
     */
    public function actionUpdateClinic()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new ClinicForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinic->edit($model);
    }

    /**
     * @api {post} /v1/clinic/index/delete 删除诊室
     * @apiName delete
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             诊室ID
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     诊室ID
     */
    public function actionDeleteClinic()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new ClinicForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinic->delete($model);
    }

    /**
     * @api {post} /v1/clinic/index/update-status 编辑诊室状态
     * @apiName update-status
     * @apiGroup Clinic
     *
     * @apiParam {Number} id                             诊室ID
     * @apiParam {Number} status                         状态【1启用、0停用】
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.id                     诊室ID
     */
    public function actionUpdateClinicStatus()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;

        $model = new ClinicForm();
        $model->setScenario('edit_status');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinic->editStatus($model);
    }

    /**
     * @api {post} /v1/clinic/index/map-clinic-list 奕远映射诊室列表
     * @apiName map-clinic-list
     * @apiGroup Clinic
     *
     * @apiParam {Number}
     * @apiParam {String}
     * @author loujie
     */
    public function actionMapClinicList()
    {
        $list = Yii::$app->services->clinic->mapClinicList();
        return [
            'list' => $list,
            'clinic' => Yii::$app->services->clinic->findList([
                'select' => 'id,name',
                'project_id' => $this->projectId,
                'status' => Clinic::STATUS_ENABLE
            ])
        ];
    }

    /**
     * @apiName 更新奕远-白友,诊室映射关系
     * @apiGroup
     * @apiParam {Number}
     * @apiParam {String}
     * @author loujie
     */
    public function actionMapClinicUpdate()
    {
        $params = Yii::$app->request->post();

        return Yii::$app->services->clinic->mapClinicUpdate($params);
    }


}
